﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Telerik.Web.UI;

namespace EXAM.Admin.Page_Admin
{
    public partial class AnalysisCode : System.Web.UI.Page
    {
        string sErr = "";
        string _db = "EXA";
        protected void Page_Load(object sender, EventArgs e)
        {

        }



        protected void btnNew_Click(object sender, EventArgs e)
        {
            RefreshForm("");
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            DTO.SSRFANVInfo ansInf = new DTO.SSRFANVInfo();
            ansInf = GetDataFromForm(ansInf);
            BUS.SSRFANVControl ctr = new BUS.SSRFANVControl();
            if (ctr.IsExist(_db, cbbCATEGORY.SelectedValue, txtCODE.Text))
            {
                sErr = ctr.Update(ansInf);
                if (sErr != "")
                    lbErr.Text = sErr;
                else lbErr.Text = "Update successfull";
            }
            else
            {
                txtID.Text = ctr.Add(ansInf, ref sErr).ToString();
                if (sErr != "")
                    lbErr.Text = sErr;
                else lbErr.Text = "Insert successfull";
            }
            if (sErr == "")
                dgvList.Rebind();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            foreach (GridItem item in dgvList.SelectedItems)
            {
                if (item.ItemIndex >= 0)
                {
                    string cate = item.OwnerTableView.DataKeyValues[item.ItemIndex]["CATEGORY"].ToString();
                    string code = item.OwnerTableView.DataKeyValues[item.ItemIndex]["CODE"].ToString();
                    BUS.SSRFANVControl ctr = new BUS.SSRFANVControl();
                    sErr = ctr.Delete(_db, cate, code);
                }
            }
            dgvList.Rebind();
        }

        private void RefreshForm(string val)
        {
            txtID.Text = val;
            txtCODE.Text = val;
            txtNAME.Text = val;
            txtLOOKUP.Text = val;
            txtBUDGET_CHECK.Text = val;

            cbPROHB_POST.Checked = false;
            cbbCATEGORY.SelectedIndex = 0;

            cbbDATA_1.SelectedIndex = 0;
            cbBUDGET_STOP.Checked = false;
            lbErr.Text = val;
        }

        private DTO.SSRFANVInfo GetDataFromForm(DTO.SSRFANVInfo ansInf)
        {
            ansInf.SUN_DB = _db;
            ansInf.CATEGORY = cbbCATEGORY.SelectedValue.ToString();
            ansInf.CODE = txtCODE.Text;
            ansInf.NAME = txtNAME.Text;
            ansInf.LOOKUP = txtLOOKUP.Text;
            ansInf.DATA_1 = cbbDATA_1.SelectedValue.ToString();

            if (cbPROHB_POST.Checked)
            {
                ansInf.PROHB_POST = "Y";
            }
            else
                ansInf.PROHB_POST = "N";

            if (cbBUDGET_STOP.Checked)
            {
                ansInf.BUDGET_STOP = "N";
            }
            else
                ansInf.BUDGET_STOP = "Y";


            ansInf.BUDGET_CHECK = txtBUDGET_CHECK.Text;


            return ansInf;
        }

        private void SetDataToForm(DTO.SSRFANVInfo ansInf)
        {
            //  txtID.Text = ansInf.ID.ToString();
            //cbbCATEGORY.SelectedValue = ansInf.CATEGORY.ToString().Trim();
            cbbCATEGORY.Text = ansInf.CATEGORY.ToString().Trim();
            txtCODE.Text = ansInf.CODE;
            txtNAME.Text = ansInf.NAME;
            txtLOOKUP.Text = ansInf.LOOKUP;
            txtBUDGET_CHECK.Text = ansInf.BUDGET_CHECK;

            if (ansInf.PROHB_POST == "Y")
            {
                cbPROHB_POST.Checked = true;

            }
            else
                cbPROHB_POST.Checked = false;

            if (ansInf.BUDGET_STOP == "Y")
            {
                cbBUDGET_STOP.Checked = true;

            }
            else
                cbBUDGET_STOP.Checked = false;



        }

        protected void dgvList_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName.ToUpper() == "SELECTED")
            {
                string cate = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["CATEGORY"].ToString();
                string code = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["CODE"].ToString();
                BUS.SSRFANVControl ctr = new BUS.SSRFANVControl();
                SetDataToForm(ctr.Get(_db, cate, code, ref sErr));
                lbErr.Text = "";
            }

        }

        protected void dgvList_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            BUS.SSRFANVControl ctr = new BUS.SSRFANVControl();
            dgvList.DataSource = ctr.GetAll(_db, ref sErr);
        }

        protected void cbbCATEGORY_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
        {
            //string sErr = "";
            BUS.ANDControl contractCtrl = new BUS.ANDControl();
            DataTable dt = contractCtrl.GetAll(_db, ref sErr);
            foreach (DataRow dataRow in dt.Rows)
            {
                RadComboBoxItem item = new RadComboBoxItem();

                item.Text = dataRow["CATEGORY"].ToString().Trim();
                item.Value = dataRow["CATEGORY"].ToString().Trim();

                string Description = (string)dataRow["DESCRIPTION"];

                item.Attributes.Add("Description", Description.ToString());

                //item.Value += ":" + unitPrice;

                cbbCATEGORY.Items.Add(item);
                item.DataBind();

            }
        }



    }
}
